package com.guigu.ssq.util;


import com.guigu.ssq.mapper.SsqMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MapperFactory {
    static SqlSession sqlSession;

    {
        String resource = "mybatis.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sqlSessionFactory.openSession(true);

    }
    public SsqMapper getMapper() throws IOException {

        SsqMapper mapper = sqlSession.getMapper(SsqMapper.class);
        return mapper;
    }
    public void closeSession(){
        sqlSession.close();
    }
}
